<?php

/**
 * This is the model class for table "frontuser".
 *
 * The followings are the available columns in table 'frontuser':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $activation_key
 * @property integer $create_time
 * @property integer $status
 */
class Frontuser extends CActiveRecord
{
    
    public $repeatpassword;              //Значение повтора пароля
    public $verifyCode;                  //Хранит значение капчи
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Frontuser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{frontuser}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
        return array(
            array('username, password', 'required'),                                //Для аутентификации Front
            
            array('email, repeatpassword', 'required', 'on'=>'registration'),      //Для регистрации Front
            array('username, email', 'unique', 'on'=>'registration'),
            array('email', 'email', 'on'=>'registration'),
            array('password', 'compare', 'compareAttribute'=>'repeatpassword', 'on'=>'registration'), 
            array('username', 'length', 'min'=>2, 'max'=>30, 'on'=>'registration'),
            array('password', 'length', 'max'=>32, 'on'=>'registration'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'registration'),  //CAPTCHA
           
           array('status', 'numerical', 'integerOnly'=>true, 'on'=>'adminchange'), //Для админки
     
            array('id, username, password, email, activation_key, create_time, status', 'safe', 'on'=>'search'),
        );
      
	}


	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Имя пользователя',
			'password' => 'Пароль',
            'repeatpassword'=>'Повторить пароль',
			'email' => 'Email',
			'activation_key' => 'Ключ регистрации',
			'create_time' => 'Дата регистрации',
			'status' => 'Статус',
            'verifyCode'=>'Код проверки',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
//		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
//		$criteria->compare('activation_key',$this->activation_key,true);
//		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    
    
    public function beforeSave()
    {
        if($this->isNewRecord)
        {
            $this->activation_key = substr(md5(uniqid(rand(), true)), 0, rand(10, 15));       //Создаём уникальный ключ активации
            $this->password      = md5(md5($this->password));                                 //Создаём хэш пароля 
            $this->create_time = time();                                                      //Указываем текущее время создания 
            $this->status = 0;                                                                //Пользователь забанен по умолчанию 
        }
        return parent::beforeSave();
    }
    
}